package com.yz.lzq.liwushuo.ui.modules.gift.tuijian;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ScrollView;
import android.widget.Toast;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.view.SimpleDraweeView;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;
import com.yz.lzq.liwushuo.R;
import com.yz.lzq.liwushuo.customview.NoScrollRecyclerView;
import com.yz.lzq.liwushuo.decoration.SpacesItemDecoration;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by AdminJia on 2016/11/25.
 */
public class GiftTuijianFragment extends Fragment implements IGiftTuiJianView {

    @Bind(R.id.simpleDraweeViw_tuiJian_gift)
    SimpleDraweeView simpleDraweeViwTuiJianGift;


    @Bind(R.id.scoreView_homepage_home)
    PullToRefreshScrollView scoreViewHomepageHome;
    @Bind(R.id.noScrollRecyclerView_tuijian_gift)
    NoScrollRecyclerView noScrollRecyclerViewTuijianGift;
    private GiftTuiJianPresenter presenter;
    private Context mContext = null;
    private int index = 0;
    private boolean flag_down = true;
    private boolean flag_up = true;

    private GiftTuiJianAdapter adapter = null;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getContext();
        Fresco.initialize(mContext);
        presenter = new GiftTuiJianPresenter(this, mContext);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_gift_tuijian, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        final GridLayoutManager manager = new GridLayoutManager(mContext, 2);

        noScrollRecyclerViewTuijianGift.setLayoutManager(manager);

        noScrollRecyclerViewTuijianGift.setNestedScrollingEnabled(false);

        presenter.loadData(index);
//        noScrollRecyclerViewTuijianGift.addOnScrollListener(new RecyclerView.OnScrollListener() {
//            @Override
//            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
//                super.onScrollStateChanged(recyclerView, newState);
//                if (lastVisibleItemPosition == adapter.getItemCount() - 1) {
//
//                    if (index <= 40) {
//                        index += 20;
//                        presenter.loadMOreData(index);
//                        scoreViewHomepageHome.onRefreshComplete();
//                    }
//
//                } else {
//                    Toast.makeText(mContext, "到底啦", Toast.LENGTH_SHORT).show();
//
//                }
//            }
//
//            @Override
//            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
//                super.onScrolled(recyclerView, dx, dy);
//                lastVisibleItemPosition = manager.findLastVisibleItemPosition();
//            }
//        });
        scoreViewHomepageHome.setMode(PullToRefreshBase.Mode.BOTH);
//        scoreViewHomepageHome.addView();
//        scoreViewHomepageHome.setShowViewWhileRefreshing();
//        scoreViewHomepageHome.removeView();
        scoreViewHomepageHome.setOnRefreshListener(new PullToRefreshBase
                .OnRefreshListener2<ScrollView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ScrollView> refreshView) {
                index = 0;
                if (flag_down) {
                    flag_down = false;
                    presenter.refrashData(index);
                    scoreViewHomepageHome.onRefreshComplete();
                    Toast.makeText(mContext, "刷新了", Toast.LENGTH_SHORT).show();
                    flag_down = true;
                }


            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ScrollView> refreshView) {

                if (flag_up) {
                    flag_up = false;
                    Toast.makeText(mContext, "上啦了", Toast.LENGTH_SHORT).show();
                    index += 20;
                    if (index <= 40) {
                        presenter.loadMOreData(index);

                    } else {
                        Toast.makeText(mContext, "到底啦", Toast.LENGTH_SHORT).show();
                    }
                    flag_up = true;
                }

                scoreViewHomepageHome.onRefreshComplete();
            }
        });


    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void setAdapterSuccess(GiftTuiJianAdapter _adapter) {
        adapter = _adapter;
        noScrollRecyclerViewTuijianGift.addItemDecoration(new SpacesItemDecoration(5));
        noScrollRecyclerViewTuijianGift.setAdapter(_adapter);
    }

    @Override
    public void setAdapterFailed(Throwable t) {
        t.printStackTrace();
    }

    @Override
    public void setImage(String string) {
        simpleDraweeViwTuiJianGift.setImageURI(Uri.parse(string));
    }
}
